﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccompanyCabinet.Common;
using AccompanyCabinet.Model;
using AccompanyCabinet.Interface;
namespace AccompanyCabinet.Front
{
    public partial class FileDownloadForm : Form
    {
        private readonly IMessageProvider _MessageProvider;
        public FileDownloadForm(IMessageProvider messageProvider )
        {
            _MessageProvider = messageProvider;
        }
        public FileDownloadForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 忙标志
        /// </summary>
        public static bool IsBusy { get; set; } = false;

        private void FileDownloadForm_Load(object sender, EventArgs e)
        {
            string fileListUrl = VideoHelper.VideoListAddress;
            if(string.IsNullOrEmpty(fileListUrl))
            {
                _MessageProvider.Show("视频服务器地址未配置");
                return;
            }
            string savedDirectory = VideoHelper.VideoCacheFileDirectory;
            if (string.IsNullOrEmpty(savedDirectory))
            {
                _MessageProvider.Show("视频缓存地址未配置");
                return;
            }
            string message;
            int code;
            List<RemoteFileInfo> fileInfo = FileHandler.GetRemoteFileList(fileListUrl, out message, out code);
            if(fileInfo == null)
            {
                return;
            }
            if(code!=0)
            {
                _MessageProvider.Show(message);
                return;
            }
            ConcurrentQueue<RemoteFileInfo> remoteFileInfos = new ConcurrentQueue<RemoteFileInfo>();
            foreach (var item in fileInfo)
            {
                string currentFileFullName = Path.Combine(savedDirectory, item.FileName);
                //检查文件是否存在，存在的文件不显示
                if (File.Exists(currentFileFullName) && (ulong)new FileInfo(currentFileFullName).Length == item.FileSize)
                {
                    continue;
                }
                remoteFileInfos.Enqueue(item);
            }
            //SingleFileDownload fileDownload = new SingleFileDownload(savedDirectory, Global.Default.VideoDownloadAddress, remoteFileInfos);
            MulitPartionDownload fileDownload = new MulitPartionDownload(new Provider.CustomeMessageProvider(this),remoteFileInfos,VideoHelper.VideoDownloadAddress);

            fileDownload.FileDownPercentageChangedEventHandler += FileDownload_FilePercentageChanged;
            fileDownload.SynchronizedTempFileEventHandler += FileDownload_SynchronizedTempFileEventHandler;
            fileDownload.Run();
            //fileDownload.FileDownloadProgressChanged += FileDownload_FileDownloadProgressChanged;
            //bool result = fileDownload.DownloadFile();
            //if (!result)
            //{
            //    MessageBox.Show(this, "系统提示", "文件下载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }

        private void FileDownload_SynchronizedTempFileEventHandler(object sender, SynchronizedTempFileEventArgs e)
        {
            this.lblCurrent.Text = $"上次传输进度：{e.CurrentFilePercentage}%";
            this.pgBarCurrent.Value = e.CurrentFilePercentage;

            this.lblTotal.Text = $"上次总体进度：{e.TotalFilesPercentage}%";
            this.pgBarTotal.Value = e.TotalFilesPercentage;

        }

        private void FileDownload_FilePercentageChanged(object sender, FileDownPercentageChangedEventArgs e)
        {
            this.lblCurrent.Text = $"当前进度：{e.CurrentFilePercentage}%";
            this.pgBarCurrent.Value = e.CurrentFilePercentage;

            this.lblTotal.Text = $"总体进度：{e.TotalFilesPercentage}%";
            this.pgBarTotal.Value = e.TotalFilesPercentage;
            if (e.CurrentFilePercentage == 100 && e.TotalFilesPercentage == 100)
            {
                this.Close();
            }
            FileHandler.WriteTempFile(e.PartionFileInfo, e.PartionIndex, e.Data);

        }

        //private void FileDownload_FileDownloadProgressChanged(object sender, FileDownloadProgressChangedEventArgs e)
        //{
        //    this.lblCurrent.Text = $"当前进度：{e.CurrentPercentage}%";
        //    this.pgBarCurrent.Value = e.CurrentPercentage;

        //    this.lblTotal.Text = $"总体进度：{e.TotalPercentage}%";
        //    this.pgBarTotal.Value = e.TotalPercentage;
        //    if(e.CurrentPercentage == 100 && e.TotalPercentage == 100)
        //    {
        //        this.Close();
        //    }
        //}
    }
}
